package com.nidu.demo.file.executor.query;

import com.alibaba.cola.dto.SingleResponse;
import com.alibaba.cola.exception.BizException;
import com.nidu.demo.common.enums.FileStorageTypeEnum;
import com.nidu.demo.common.exception.ErrorCodeConstants;
import com.nidu.demo.file.convertor.FileConfigConvertor;
import com.nidu.demo.file.dto.FileConfigCO;
import com.nidu.demo.file.gateway.FileConfigGateway;
import com.nidu.demo.file.model.FileConfig;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Component;

/**
 * 文件配置单个查询执行器
 *
 * @author auto
 */
@Component
@RequiredArgsConstructor
public class FileConfigSingleQryExe {

    private final FileConfigGateway fileConfigGateway;
    private final FileConfigConvertor fileConfigConvertor;

    /**
     * 根据ID查询文件配置
     *
     * @param id 文件配置ID
     * @return 文件配置信息
     */
    public SingleResponse<FileConfigCO> execute(Long id) {
        FileConfig fileConfig = fileConfigGateway.getById(id);
        if (fileConfig == null) {
            throw new BizException(ErrorCodeConstants.FILE_CONFIG_NOT_FOUND.getCode(), ErrorCodeConstants.FILE_CONFIG_NOT_FOUND.getMessage());
        }
        
        FileConfigCO fileConfigCO = fileConfigConvertor.toClientObject(fileConfig);
        
        // 设置存储类型名称
        try {
            FileStorageTypeEnum storageType = FileStorageTypeEnum.valueOf(fileConfig.getStorage());
            fileConfigCO.setStorageName(storageType.getName());
        } catch (Exception e) {
            fileConfigCO.setStorageName("未知类型");
        }
        
        return SingleResponse.of(fileConfigCO);
    }
}
